/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package note.gui;

import note.vo.Note;
import note.dao.NoteDAO;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author tai
 */
public class AllNoteFrame extends javax.swing.JFrame {

    /**
     * Creates new form lx2
     */
    public AllNoteFrame() {
        initComponents();
        show_all();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        showAllButton = new javax.swing.JButton();
        addButton = new javax.swing.JButton();
        saveButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        exitButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        clearButton = new javax.swing.JButton();
        findButton = new javax.swing.JButton();
        txtFind = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        txtTitle = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        txtAuthor = new javax.swing.JTextField();
        comboFind = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        comboId = new javax.swing.JComboBox();
        findIdButton = new javax.swing.JButton();
        saveAllButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        showAllButton.setText("重新显示");
        showAllButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showAllButtonActionPerformed(evt);
            }
        });

        addButton.setText("添加");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        saveButton.setText("保存记录");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        deleteButton.setText("删除记录");
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        exitButton.setText("退出");
        exitButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "留言ID", "标题", "作者", "内容"
            }
        ));
        jScrollPane1.setViewportView(table);

        clearButton.setText("清除");
        clearButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearButtonActionPerformed(evt);
            }
        });

        findButton.setText("查找");
        findButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("标题");

        jLabel2.setText("作者");

        comboFind.setText("组合查找");
        comboFind.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                comboFindActionPerformed(evt);
            }
        });

        comboId.addAncestorListener(new javax.swing.event.AncestorListener() {
            public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
            }
            public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                comboIdAncestorAdded(evt);
            }
            public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
            }
        });

        findIdButton.setText("查找");
        findIdButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findIdButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(comboId, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(findIdButton)
                .addContainerGap(23, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(findIdButton))
                .addContainerGap(69, Short.MAX_VALUE))
        );

        saveAllButton.setText("保存全部");
        saveAllButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveAllButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 452, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 27, Short.MAX_VALUE)
                        .addComponent(saveAllButton)
                        .addGap(15, 15, 15))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(96, 96, 96)
                                .addComponent(txtFind, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(59, 59, 59)
                                .addComponent(findButton))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(25, 25, 25)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addGap(18, 18, 18)
                                        .addComponent(txtAuthor, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel1)
                                        .addGap(18, 18, 18)
                                        .addComponent(txtTitle, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(10, 10, 10)
                                .addComponent(comboFind)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 89, Short.MAX_VALUE)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(33, 33, 33))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(19, 19, 19)
                        .addComponent(showAllButton)
                        .addGap(33, 33, 33)
                        .addComponent(addButton)
                        .addGap(33, 33, 33)
                        .addComponent(saveButton)
                        .addGap(18, 18, 18)
                        .addComponent(deleteButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(clearButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 58, Short.MAX_VALUE)
                        .addComponent(exitButton)))
                .addGap(33, 33, 33))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(findButton)
                            .addComponent(txtFind, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel1)
                                    .addComponent(txtTitle, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel2)
                                    .addComponent(txtAuthor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(7, 7, 7))
                            .addComponent(comboFind)))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(saveAllButton)
                        .addGap(38, 38, 38)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(showAllButton)
                    .addComponent(addButton)
                    .addComponent(saveButton)
                    .addComponent(deleteButton)
                    .addComponent(exitButton)
                    .addComponent(clearButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        //int rowCount = model.getRowCount();
        model.addRow(new String[]{null, null, null, null});


}//GEN-LAST:event_addButtonActionPerformed

    private void clearButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearButtonActionPerformed
        // TODO add your handling code here:
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        int rowCount = table.getRowCount();
        for (int i = 0; i < rowCount; i++) {
            model.removeRow(rowCount - i - 1);
        }
}//GEN-LAST:event_clearButtonActionPerformed

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        if (table.getSelectedRow() == -1) {
            JOptionPane.showMessageDialog(this, "不能选择空行");
            return;
        }
        String id = (String) table.getValueAt(table.getSelectedRow(), 0);
        if (id == null || id.trim().equals("")) {
            JOptionPane.showMessageDialog(this, "请注意id值");
            return;
        }

        int result = JOptionPane.showConfirmDialog(this, "请确认是否删除");
        if (result == JOptionPane.NO_OPTION || result == JOptionPane.CANCEL_OPTION) {
            return;
        }
        NoteDAO noteDAO = new NoteDAO();
        if (noteDAO.delete(Integer.parseInt(id))) {
            JOptionPane.showMessageDialog(this, "删除成功");
        } else {
            JOptionPane.showMessageDialog(this, "删除失败");
        }
        int index = table.getSelectedRow();
        if (index >= 0) {
            DefaultTableModel model = (DefaultTableModel) table.getModel();
            model.removeRow(index);
        }
    }//GEN-LAST:event_deleteButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        String id1 =(String) table.getValueAt(table.getSelectedRow(), 0);
        if (id1 == null) {
            JOptionPane.showMessageDialog(this, "请输入ID");
            return;
        }
        
        String title  =(String) table.getValueAt(table.getSelectedRow(), 1);
        if (title == null) {
            JOptionPane.showMessageDialog(this, "请输入标题");
            return;
        }
      
         String author = (String)table.getValueAt(table.getSelectedRow(), 2);
        if (author == null) {
            JOptionPane.showMessageDialog(this, "请输入作者");
            return;
        }
       
        String content =(String) table.getValueAt(table.getSelectedRow(), 3);
        if (content == null) {
            JOptionPane.showMessageDialog(this, "请输入内容");
            return;
        }
       
        NoteDAO noteDAO = new NoteDAO();

        if (id1 == null || "".equals(id1)) {
            JOptionPane.showMessageDialog(this, "不能选择空行");
            return;
        }
        int id = Integer.parseInt(id1);
        if (noteDAO.queryById(id) != null) {
            if (noteDAO.update(id, title, author, content)) {
                JOptionPane.showMessageDialog(this, "修改成功");
            } else {
                JOptionPane.showMessageDialog(this, "修改失败");
            }
        } else {
            if (noteDAO.insert(id, title, author, content)) {
                JOptionPane.showMessageDialog(this, "添加成功");
            } else {
                JOptionPane.showMessageDialog(this, "添加失败");
            }
        }
    }//GEN-LAST:event_saveButtonActionPerformed

    private void showAllButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showAllButtonActionPerformed
        // TODO add your handling code here:
        // clearButtonActionPerformed(evt);
        show_all();
    }//GEN-LAST:event_showAllButtonActionPerformed

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitButtonActionPerformed
        this.dispose();
    }//GEN-LAST:event_exitButtonActionPerformed

    private void findButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findButtonActionPerformed
        // TODO add your handling code here:
        if (txtFind.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "请输入查找内容");
            return;
        }
        clearButtonActionPerformed(evt);
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        NoteDAO noteDAO = new NoteDAO();
        String[][] p = noteDAO.findByLike(txtFind.getText());
        model.setDataVector(p, new String[]{
            "留言ID", "标题", "作者", "内容"
        });

    }//GEN-LAST:event_findButtonActionPerformed

    private void comboFindActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboFindActionPerformed
        // TODO add your handling code here:
        String title = this.txtTitle.getText();
        String author = this.txtAuthor.getText();
        if (title.equals("") && author.equals("")) {
            JOptionPane.showMessageDialog(this, "请输入查找内容");
            return;
        }
        clearButtonActionPerformed(evt);
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        NoteDAO noteDAO = new NoteDAO();
        String[][] p = noteDAO.findByCombo(title, author);
        model.setDataVector(p, new String[]{
            "留言ID", "标题", "作者", "内容"
        });
    }//GEN-LAST:event_comboFindActionPerformed

    private void comboIdAncestorAdded(javax.swing.event.AncestorEvent evt) {//GEN-FIRST:event_comboIdAncestorAdded
        // TODO add your handling code here:
        NoteDAO noteDAO = new NoteDAO();
        String data[] = noteDAO.getSingleFiledData();
        for (int i = 0; i < data.length; i++) {
            this.comboId.addItem(data[i]);
        }
    }//GEN-LAST:event_comboIdAncestorAdded

    private void findIdButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findIdButtonActionPerformed
        String id = (String) this.comboId.getSelectedItem();
        NoteDAO noteDAO = new NoteDAO();
        Note note = noteDAO.queryById(Integer.parseInt(id));
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        String content[][]={{String.valueOf(note.getId()),
            note.getTitle(),note.getAuthor(),note.getContent()}};
        model.setDataVector(content, new String[]{
            "留言ID", "标题", "作者", "内容"
        });

    }//GEN-LAST:event_findIdButtonActionPerformed

    private void saveAllButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveAllButtonActionPerformed

        NoteDAO noteDAO = new NoteDAO();
        noteDAO.saveTableData(table);

    }//GEN-LAST:event_saveAllButtonActionPerformed
    private void show_all() {
// TODO 将在此处添加您的处理代码：
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        NoteDAO noteDAO = new NoteDAO();
        String[][] p = noteDAO.getAllData();
        model.setDataVector(p, new String[]{
            "留言ID", "标题", "作者", "内容"
        });
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new AllNoteFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JButton clearButton;
    private javax.swing.JButton comboFind;
    private javax.swing.JComboBox comboId;
    private javax.swing.JButton deleteButton;
    private javax.swing.JButton exitButton;
    private javax.swing.JButton findButton;
    private javax.swing.JButton findIdButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton saveAllButton;
    private javax.swing.JButton saveButton;
    private javax.swing.JButton showAllButton;
    private javax.swing.JTable table;
    private javax.swing.JTextField txtAuthor;
    private javax.swing.JTextField txtFind;
    private javax.swing.JTextField txtTitle;
    // End of variables declaration//GEN-END:variables
}
